package com.example.ywr.repository;

import com.example.ywr.entity.Category;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 商品分类数据访问层
 */
@Repository
public interface CategoryRepository extends JpaRepository<Category, Long> {

    /**
     * 根据父分类ID查询子分类
     */
    List<Category> findByParentIdAndIsDeletedFalse(Long parentId);

    /**
     * 根据层级查询分类
     */
    List<Category> findByLevelAndIsDeletedFalse(Integer level);

    /**
     * 根据名称模糊查询
     */
    Page<Category> findByNameContainingAndIsDeletedFalse(String name, Pageable pageable);

    /**
     * 更新分类状态
     */
    @Modifying
    @Query("UPDATE Category c SET c.status = ?2 WHERE c.id = ?1")
    int updateStatus(Long id, Integer status);

    /**
     * 批量更新分类状态
     */
    @Modifying
    @Query("UPDATE Category c SET c.status = ?2 WHERE c.id IN ?1")
    int batchUpdateStatus(List<Long> ids, Integer status);
}